package com.wxzz.elearing.user.api.app;


import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.user.dto.SystemMessageDto;
import com.wxzz.elearing.user.entity.SystemMessage;
import com.wxzz.elearing.user.service.ISystemMessageService;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 消息推送 前端控制器
 * </p>
 *
 * @author
 * @since 2021-05-21
 */
@RestController
@RequestMapping("/wx-system-message")
@Api(tags = "消息通知(小程序)")
public class WxSystemMessageController {

    @Autowired
    private ISystemMessageService systemMessageService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "通知列表", notes = "通知列表", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "wxListSystemMessage", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<SystemMessage>>> wxListSystemMessage(@RequestHeader String token, @RequestBody SystemMessageDto systemMessageDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return systemMessageService.wxListSystemMessage(jsonObject.getLong("id"),systemMessageDto);
    }


    @ApiOperation(value = "根据id查询消息详情", notes = "根据id查询消息详情", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "wxSelectSystemMessageById/{systemMessageId}", method = RequestMethod.POST)
    public ReturnVO<SystemMessage> selectSystemMessageById(@RequestHeader String token,@PathVariable Long systemMessageId) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return systemMessageService.wxSelectSystemMessageById(jsonObject.getLong("id"),systemMessageId);
    }

    @ApiOperation(value = "一键已读(小程序)", notes = "一键已读(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "wxAllRead", method = RequestMethod.POST)
    public ReturnVO wxAllRead(@RequestHeader String token) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return systemMessageService.wxAllRead(jsonObject.getLong("id"));
    }

}
